
/**
 * @file
 * Enhancements for select list configuration options.
 */
 
function exec_sql() {
  //alert('11');
}

(function ($) {

Drupal.behaviors.webformSelectLoadOptions = {};
Drupal.behaviors.webformSelectLoadOptions.attach = function(context) {
  settings = Drupal.settings;
  
  //-----------------------Fix the IE 'readonly' return bug-------------------------------
  $('input[readonly]', context).keydown(function(e) {
    //e.preventDefault();
	if(e.keyCode==8){  
      return false; 
    }
  });
  $('input[readonly]', context).each(function(){
	$(this).css("color","#999");
  });
  $('textarea[readonly]', context).keydown(function(e) {
    //e.preventDefault();
	if(e.keyCode==8){  
      return false; 
    }
  });
  $('textarea[readonly]', context).each(function(){
	$(this).css("color","#999");
  });
  //--------------------------------------End----------------------------------------------
  
  //Inital the 'edit-extra-sqlitems' area
  if (settings.webform.ajaxcid<=0) {
	//For new page    
   $('#edit-extra-sqlitems').fadeOut(0);
  }
  else {
	//For edit page  
  	$('#edit-extra-sqlitems').fadeOut(0);
	if (settings.webform.options_source=='dynamic_dabatase') {
      $('#edit-extra-sqlitems').fadeIn(0);
    }
  }
  
  // General the 'extra-items' data by real time
  if (settings.webform.options_source=='dynamic_dabatase') {
    var url = settings.webform.dynamicDBUrl;
    var db = $("#edit-extra-sqlitems-db").val();
	var sql = $("#edit-extra-sqlitems-sql-input").val();
	var data = 'db='+db+'&sql='+sql;
	
	$.ajax({
      url: url,
	  type: "POST",
	  data: data,
      success: Drupal.webform.selectOptionsLoad,
      dataType: 'json'
    });	
  }

  $('#edit-extra-options-source', context).change(function() {
	// Hiden or show 'edit-extra-sqlitems' area
	if (this.value=='dynamic_dabatase') {
	  $('#edit-extra-sqlitems').fadeIn(0);
	}
	else {
	  $('#edit-extra-sqlitems').fadeOut(0);
	}
	
	var ajaxcid = settings.webform.ajaxcid;  
    //var url = settings.webform.selectOptionsUrl + '/' + this.value;
	var url = settings.webform.selectOptionsUrl + '/' + this.value + '/' + ajaxcid;
	$.ajax({
      url: url,
      success: Drupal.webform.selectOptionsLoad,
      dataType: 'json'
    });
  });
  
  $('#edit-sqlitems-exec-sql', context).click(function() {
    var url = settings.webform.dynamicDBUrl;
    var db = $("#edit-extra-sqlitems-db").val();
	var sql = $("#edit-extra-sqlitems-sql-input").val();
	var data = 'db='+db+'&sql='+sql; //alert(url);alert(data);
	if (db=='') {
	  alert('please select a dynamic dabatase');
	  $("#edit-extra-sqlitems-db").focus();
	  return false;
	}
	if (sql=='') {
	  alert('please input the database query sql');
	  $("#edit-extra-sqlitems-sql-input").focus();
	  return false;
	}
	$.ajax({
      url: url,
	  type: "POST",
	  data: data,
	  error: function(){
          alert('There are errors in you query sql');
      },
      success: Drupal.webform.selectOptionsLoad,
      dataType: 'json'
    });
  });
  
}

Drupal.webform = Drupal.webform || {};

Drupal.webform.selectOptionsOriginal = false;
Drupal.webform.selectOptionsLoad = function(result) {
  if (Drupal.optionsElement) {
    if (result.options) {
      // Save the current select options the first time a new list is chosen.
      if (Drupal.webform.selectOptionsOriginal === false) {
        Drupal.webform.selectOptionsOriginal = $(Drupal.optionElements[result.elementId].manualOptionsElement).val();
      }
      $(Drupal.optionElements[result.elementId].manualOptionsElement).val(result.options);
      Drupal.optionElements[result.elementId].disable();
      Drupal.optionElements[result.elementId].updateWidgetElements();
    }
    else {
      Drupal.optionElements[result.elementId].enable();
      if (Drupal.webform.selectOptionsOriginal) {
        $(Drupal.optionElements[result.elementId].manualOptionsElement).val(Drupal.webform.selectOptionsOriginal);
        Drupal.optionElements[result.elementId].updateWidgetElements();
        Drupal.webform.selectOptionsOriginal = false;
      }
    }
  }
  else {
    if (result.options) {
      $('#' + result.elementId).val(result.options).attr('readonly', 'readonly');
	  //-------------------------Add to for the readonly function--------------------------------------------
	  $('#' + result.elementId).css("color", "#999");
	  $('#' + result.elementId).keydown(function(e) {
    	//e.preventDefault();
		if(e.keyCode==8 && document.getElementById(result.elementId).readOnly==true){
		  //e.keyCode = 0;
		  //e.returnValue = false;	//bug: ie url will return back
		  return false;
    	}
  	  });
	  //-------------------------------------------------------------------------------------------------------
    }
    else {
      $('#' + result.elementId).val('').attr('readonly', '');
	  //-------------------------Add to cancel the readonly function--------------------------------------------
	  $('#' + result.elementId).css("color", "#000");
	  $('#' + result.elementId).keydown(function(e) {
        if(e.keyCode==8){
          e.returnValue = true; 
    	}
  	  });
	  //----------------------------------------------------------------------------------------------------------
    }
  }
}

})(jQuery);
